﻿using System.Threading.Tasks;

namespace Sidvall.Office
{
    public class Excel : Sidvall.Office.IExcel
    {
        #region Public Members

        #region CreateSolidFill

        public object CreateSolidFill(Color color)
        {
            Infragistics.Documents.Excel.WorkbookColorInfo workbookColorInfo;

            if (color != null)
            {
                workbookColorInfo = new Infragistics.Documents.Excel.WorkbookColorInfo(System.Drawing.Color.FromArgb(color.Alpha, color.Red, color.Green, color.Blue));
                return Infragistics.Documents.Excel.CellFill.CreateSolidFill(workbookColorInfo);
            }
            else
            {
                return Infragistics.Documents.Excel.CellFill.NoColor;
            }
        }

        #endregion
        #region CreateWorkbookColorInfo

        public object CreateWorkbookColorInfo(Color color, double tint)
        {
            if (color != null)
            {
                return new Infragistics.Documents.Excel.WorkbookColorInfo(System.Drawing.Color.FromArgb(color.Alpha, color.Red, color.Green, color.Blue), tint);
            }
            else
            {
                return Infragistics.Documents.Excel.WorkbookColorInfo.Automatic;
            }
        }

        #endregion
        #region LoadWorkbookAsync

        public async Task<IWorkbook> LoadWorkbookAsync(System.IO.Stream stream)
        {
            Infragistics.Documents.Excel.Workbook item;

            item = Infragistics.Documents.Excel.Workbook.Load(stream);

            return await Task.FromResult(new Workbook(item));
        }

        #endregion
        #region NewWorkbookAsync

        public async Task<IWorkbook> NewWorkbookAsync(WorkbookFormat format)
        {
            Infragistics.Documents.Excel.Workbook item;

            item = new Infragistics.Documents.Excel.Workbook(GetWorkbookFormat(format));

            return await Task.FromResult(new Workbook(item));
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return "Excel - Sidvall.Office.IG";
        }

        #endregion

        #endregion
        #region Private Members

        #region GetTextFormatMode

        internal static Infragistics.Documents.Excel.TextFormatMode GetTextFormatMode(TextFormatMode mode)
        {
            switch (mode)
            {
                case TextFormatMode.AsDisplayed:
                    return Infragistics.Documents.Excel.TextFormatMode.AsDisplayed;
                case TextFormatMode.IgnoreCellWidth:
                    return Infragistics.Documents.Excel.TextFormatMode.IgnoreCellWidth;
                default:
                    throw new System.ArgumentException("Invalid mode");
            }
        }

        #endregion
        #region GetWorkbookFormat

        internal static Infragistics.Documents.Excel.WorkbookFormat GetWorkbookFormat(WorkbookFormat format)
        {
            switch (format)
            {
                case WorkbookFormat.Excel2007:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel2007;
                case WorkbookFormat.Excel2007MacroEnabled:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel2007MacroEnabled;
                case WorkbookFormat.Excel2007MacroEnabledTemplate:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel2007MacroEnabledTemplate;
                case WorkbookFormat.Excel2007Template:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel2007Template;
                case WorkbookFormat.Excel97To2003:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel97To2003;
                case WorkbookFormat.Excel97To2003Template:
                    return Infragistics.Documents.Excel.WorkbookFormat.Excel97To2003Template;
                case WorkbookFormat.StrictOpenXml:
                    return Infragistics.Documents.Excel.WorkbookFormat.StrictOpenXml;
                default:
                    throw new System.ArgumentException("Unexpected format value:" + format.ToString());
            }
        }

        #endregion

        #region GetWorksheetColumnWidthUnit

        internal static Infragistics.Documents.Excel.WorksheetColumnWidthUnit GetWorksheetColumnWidthUnit(WorksheetColumnWidthUnit unit)
        {
            switch (unit)
            {
                case WorksheetColumnWidthUnit.Character:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.Character;
                case WorksheetColumnWidthUnit.Character256th:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.Character256th;
                case WorksheetColumnWidthUnit.CharacterPaddingExcluded:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.CharacterPaddingExcluded;
                case WorksheetColumnWidthUnit.Pixel:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.Pixel;
                case WorksheetColumnWidthUnit.Point:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.Point;
                case WorksheetColumnWidthUnit.Twip:
                    return Infragistics.Documents.Excel.WorksheetColumnWidthUnit.Twip;
                default:
                    throw new System.ArgumentException("Unexpected unit value:" + unit.ToString());
            }
        }

        #endregion

        #endregion
        #region Constructors

        public Excel()
        {
        }

        #endregion
    }
}
